package myJs.api

import org.scalajs.dom.window
import shared.pojo.Pojo._
import slinky.web.html.s
import upickle.default._
import sttp.client3._
import sttp.model._
import sttp.client3.upicklejson._

import scala.concurrent.Future
import scala.util._
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
import shared.pojo.Pojo


/**
 * Created by yz on 7/11/2022
 */
object Api {

  val apiUrl = window.location.hostname match {
    case "localhost" => "http://localhost:9000"
    case _ => s"http://${window.location.host}/liveForest"
  }

  val user = UserApi
  val admin = AdminApi
  val predict = PredictApi
  val tool = ToolApi

  def images = {
    s"${apiUrl}/assets/images"
  }

  val sttpBackend = FetchBackend()

  private def myAsJson[R: Reader] = {
    asJson[R]
      .map {
        case Right(response) =>
          Success(response)
        case Left(e) =>
          Failure(new RuntimeException(s"Got response exception:\n${e}"))
      }
  }

  def preparseRequest[R: Reader] = {
    basicRequest
      .contentType(MediaType.ApplicationJson)
      .response(myAsJson[R])
  }

  def login(request: Login.Request) = {
    val url = s"${Api.apiUrl}/liveForest/login"
    preparseRequest[Login.Response]
      .post(uri"${url}")
      .body(request)
      .send(sttpBackend)
      .map(_.body)
      .flatMap(Future.fromTry)
  }

  def register(request: RegisterUserData) = {
    val url = s"${Api.apiUrl}/liveForest/register"
    preparseRequest[ValidData]
      .post(uri"${url}")
      .body(request)
      .send(sttpBackend)
      .map(_.body)
      .flatMap(Future.fromTry)
  }

  def userNameCheck(request: UserNameData) = {
    val url = s"${Api.apiUrl}/liveForest/userNameCheck"
    preparseRequest[ValidData]
      .post(uri"${url}")
      .body(request)
      .send(sttpBackend)
      .map(_.body)
      .flatMap(Future.fromTry)
  }
  def validName(request: UserNameData) = {
    val url = s"${Api.apiUrl}/liveForest/validName"
    preparseRequest[ValidData]
      .post(uri"${url}")
      .body(request)
      .send(sttpBackend)
      .map(_.body)
      .flatMap(Future.fromTry)
  }

  def emailCheck(request: EmailData) = {
    val url = s"${Api.apiUrl}/liveForest/emailCheck"
    preparseRequest[ValidData]
      .post(uri"${url}")
      .body(request)
      .send(sttpBackend)
      .map(_.body)
      .flatMap(Future.fromTry)
  }

  def phoneCheck(request: PhoneData) = {
    val url = s"${Api.apiUrl}/liveForest/phoneCheck"
    preparseRequest[ValidData]
      .post(uri"${url}")
      .body(request)
      .send(sttpBackend)
      .map(_.body)
      .flatMap(Future.fromTry)
  }

  def sendEmail(request: EmailData) = {
    val url = s"${Api.apiUrl}/liveForest/sendEmail"
    preparseRequest[ValidData]
      .post(uri"${url}")
      .body(request)
      .send(sttpBackend)
      .map(_.body)
      .flatMap(Future.fromTry)
  }
  def newPasswordEmailValid(request: EmailData) = {
    val url = s"${Api.apiUrl}/liveForest/newPasswordEmailValid"
    preparseRequest[ValidData]
      .post(uri"${url}")
      .body(request)
      .send(sttpBackend)
      .map(_.body)
      .flatMap(Future.fromTry)
  }

  def registerActive(request: EmailData) = {
    val url = s"${Api.apiUrl}/liveForest/registerActive"
    preparseRequest[ConfirmActive.Response]
      .post(uri"${url}")
      .body(request)
      .send(sttpBackend)
      .map(_.body)
      .flatMap(Future.fromTry)
  }
  def newPasswordActive(request: UserNameData) = {
    val url = s"${Api.apiUrl}/liveForest/newPasswordActive"
    preparseRequest[String]
      .post(uri"${url}")
      .body(request)
      .send(sttpBackend)
      .map(_.body)
      .flatMap(Future.fromTry)
  }

  def newPasswordBefore(request: EmailData) = {
    val url = s"${Api.apiUrl}/liveForest/newPasswordBefore"
    preparseRequest[String]
      .post(uri"${url}")
      .body(request)
      .send(sttpBackend)
      .map(_.body)
      .flatMap(Future.fromTry)
  }

  def newPassword(request: NewPassword.Request) = {
    val url = s"${Api.apiUrl}/liveForest/newPassword"
    preparseRequest[ValidData]
      .post(uri"${url}")
      .body(request)
      .send(sttpBackend)
      .map(_.body)
      .flatMap(Future.fromTry)
  }

}
